package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.dao.SaveScalingMapper;
import com.wsd.smartcarwasher.domain.OrderInfo;
import com.wsd.smartcarwasher.domain.SaveScaling;
import com.wsd.smartcarwasher.domain.SaveScalingExample;
import com.wsd.smartcarwasher.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SavaScalingService {
    private static final Logger logger = LoggerFactory.getLogger(SavaScalingService.class);
    @Resource
    @Lazy
    private SaveScalingMapper saveScalingMapper;
    @Resource
    private SysDicService sysDicService;
    @Resource
    private OrderService orderService;

    /**
     * 根据用户id查看当前用户的省时省钱比例
     * @param userId
     * @return
     */
    public SaveScaling lookSaveScaling(String userId)
    {
        SaveScalingExample example=new SaveScalingExample();
        example.createCriteria().andUseridEqualTo(userId);
        List<SaveScaling>  saveScalingList=this.saveScalingMapper.selectByExample(example);
        if(saveScalingList.isEmpty())
        {
            SaveScaling saveScaling=new SaveScaling();
            saveScaling.setAmount(new BigDecimal(0));
            saveScaling.setHour(0);
            saveScaling.setWater(0);
            return saveScaling;
        }
        return saveScalingList.get(0);
    }

    /**
     * 为用户结算省时省钱比例
     * @param orderSn
     */
    public void createSavaScaling(String orderSn)
    {
        OrderInfo orderInfo=this.orderService.queryOrderInfoByOrderSn(orderSn);
        //查用是否有该用户下单比例记录，如果没有就生成一个
        SaveScalingExample example=new SaveScalingExample();
        example.createCriteria().andUseridEqualTo(orderInfo.getUserId());
        List<SaveScaling>  saveScalingList=this.saveScalingMapper.selectByExample(example);
        if(saveScalingList.isEmpty())
        {
            SaveScaling saveScaling=new SaveScaling();
            saveScaling.setAmount(new BigDecimal(0));
            saveScaling.setHour(0);
            saveScaling.setWater(0);
            saveScaling.setCreateTime(new Date());
            saveScaling.setOrderCount(0);
            saveScaling.setUserid(orderInfo.getUserId());
            this.saveScalingMapper.insert(saveScaling);
        }
        //取后台配置的其他卖家洗车设备的价格、耗时、耗水
        BigDecimal price=new BigDecimal(this.sysDicService.getByCode(1010).get(0).getValue());
        int hour=Integer.parseInt(this.sysDicService.getByCode(1011).get(0).getValue());
        int water=Integer.parseInt(this.sysDicService.getByCode(1012).get(0).getValue());

        saveScalingList=this.findSaveScaling(orderInfo.getUserId());
        SaveScaling saveScaling=this.saveScalingMapper.selectByPrimaryKey(saveScalingList.get(0).getId());
        //金额累积
        saveScaling.setAmount(saveScaling.getAmount().add(price));
        //时间累积
        int manwoHour=Integer.parseInt(DateUtil.longToString(new Date().getTime()-orderInfo.getCreateTime().getTime(),"HH"));
        saveScaling.setHour(saveScaling.getHour()+hour-manwoHour);
        //耗水累积
        saveScaling.setWater(water-12+saveScaling.getWater());
        //单数积累
        saveScaling.setOrderCount(saveScalingList.get(0).getOrderCount()+1);
        this.saveScalingMapper.updateByExampleSelective(saveScaling,example);
    }

    public  List<SaveScaling> findSaveScaling(String userId){
        SaveScalingExample saveScalingExample=new SaveScalingExample();
        saveScalingExample.createCriteria().andUseridEqualTo(userId);

        return this.saveScalingMapper.selectByExample(saveScalingExample);
    }

    public Map<String,Object> outSaveScaling(SaveScaling saveScaling)
    {
        Map<String,Object> map=new HashMap<>();
        map.put("water",saveScaling.getWater()+"升");
        map.put("money",saveScaling.getAmount()+"元");
        if(saveScaling.getHour()>=60)
        {
            if(saveScaling.getHour()==60)
            {
                map.put("minute",1+"小时");

            }else{
                float minute=(float)saveScaling.getHour()/(float)60;
                DecimalFormat df=new DecimalFormat("0.00");//设置保留位数
                map.put("minute",df.format(minute)+"小时");
            }

        }else {
            map.put("minute", saveScaling.getHour()+"分钟");
        }

        return map;
    }


}
